import Vue from 'vue'
import VueRouter from 'vue-router'

const Login = () => import('@/views/Login.vue')
const Home = () => import('@/views/Home.vue')
const Welcome = () => import('@/views/Welcome.vue')
const Users = () => import('@/views/user/Users.vue')
const Roles = () => import('@/views/power/Roles.vue')
const Right = () => import('@/views/power/Right.vue')
Vue.use(VueRouter)

  const routes = [
    {path:'/', redirect: '/login'},
    {path:'/login', component: Login},
    {
      path:'/home',
      component: Home,
      redirect: '/welcome',
      children:[
        {
          path:'/welcome',
          component: Welcome
        },
        {
          path: '/users',
          component: Users
        },
        {
          path: '/roles',
          component: Roles
        },
        {
          path: '/rights',
          component: Right
        }
      ]
    }
  ]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) =>{
  if(to.path == '/login') return next();
  const token = window.sessionStorage.getItem('token')
  if(!token){
    return next('/login')
  }
  return next()
})

export default router
